package qidi.gushi;


import android.os.Environment;
import android.util.Log;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
 * Created by zcj on 2017/7/27.
 */

public class LogUtils
{
    private static final String TAG = "Logcat";
    private static boolean saveLog = false;
    private static final boolean IS_TEST =  false;
    private static RandomAccessFile accessFile;
    public static boolean isDebug = true;
    //日志存储目录
    public static final String LOG_FILE = Environment.getExternalStorageDirectory() + "/accurator_log.txt";

    public LogUtils() {
        //不能实例化
        throw new UnsupportedOperationException("cannot");
    }

    public static void d(String msg) {
        if (isDebug) {
            Log.d(TAG, msg);
        }
    }

    public static void d(String TAG, String msg) {
        if (isDebug) {
            Log.d(TAG, msg);
        }
    }

    public static void i(String msg) {
        if (isDebug) {
            Log.i(TAG, msg);
        }
    }

    public static void i(String TAG, String msg) {
        if (isDebug) {
            Log.i(TAG, msg);
        }
    }

    public static void v(String msg) {
        if (isDebug) {
            Log.v(TAG, msg);
        }
    }

    public static void v(String TAG, String msg) {
        if (isDebug) {
            Log.v(TAG, msg);
        }
    }

    public static void e(String msg) {
        if (isDebug) {
            Log.e(TAG, msg);
        }
    }

    public static void e(String TAG, String msg) {
        if (isDebug) {
            Log.e(TAG, msg);
        }
    }

    public static void e(String TAG, String msg, Throwable tr) {
        if (isDebug) {
            Log.e(TAG, msg, tr);
        }
    }


    public static void w(String msg) {
        if (isDebug) {
            Log.w(TAG, msg);
        }
    }

    public static void w(String TAG, String msg) {
        if (isDebug) {
            Log.w(TAG, msg);
        }
    }

    public static void w(String TAG, Throwable msg) {
        if (isDebug) {
            Log.w(TAG, msg);
        }
    }

    public static void w(String TAG, String msg, Throwable e) {
        if (isDebug) {
            Log.w(TAG, msg, e);
        }
    }

    private static void saveLog(String msg) {
        if (!saveLog) {
            return;
        }
        if (IS_TEST)
            try {
                if (accessFile == null) {
                    accessFile = new RandomAccessFile(LOG_FILE, "rw");
                }
                long fileLength = accessFile.length();
                // 将写文件指针移到文件尾。
                accessFile.seek(fileLength);
                accessFile.writeBytes(formatDate(new Date()) + ":");
                accessFile.writeBytes(msg);
                accessFile.writeBytes("\r\n");
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
    }

    private static DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public static String formatDate(Date date) {
        return dateFormat.format(date);
    }
}
